package org.smoozik.app.common.partition.component;

import java.awt.Font;
import java.awt.Graphics;

import org.smoozik.app.common.partition.PartitionValues;
import org.smoozik.services.musictheory.Token;

@SuppressWarnings("serial")
public class UISilence extends UIMusicComponent {

	public UISilence(Token e) {
		super(e);
		// label = token.getDecomposedDurationLabel();
		label = "" + token.getDuration();
	}

	@Override
	public void paint(Graphics g) {
		y = partition.getHeight();
		int height = 0;

		if (token.getDuration() == 4) {
			height = PartitionValues.getWidth() / 2;
			y += PartitionValues.getWidth();
			g.fillRect(x, y, PartitionValues.getWidth(), height);

		} else if (token.getDuration() == 2) {
			height = PartitionValues.getWidth() / 2;
			y += 1.5 * PartitionValues.getWidth();
			g.fillRect(x, y, PartitionValues.getWidth(), height);

		} else if (token.getDuration() == 0.25) {
			y += PartitionValues.getWidth();
			drawDemiSoupir(g, x, y);
			drawDemiSoupir(g, x - PartitionValues.getWidth() / 2, y
					+ PartitionValues.getWidth());
		} else if (token.getDuration() == 0.5) {
			y += PartitionValues.getWidth();
			drawDemiSoupir(g, x, y);
		} else if (token.getDuration() == 0.75) {
			y += PartitionValues.getWidth();
			drawDemiSoupir(g, x, y);
			g.fillRect(x + PartitionValues.getWidth() + 2,
					y + PartitionValues.getWidth(),
					PartitionValues.getWidth() / 4,
					PartitionValues.getWidth() / 4);
		} else if (token.getDuration() == 1) {
			y += PartitionValues.getWidth() / 2;
			drawSoupir(g, x, y);
		} else if (token.getDuration() == 1.5) {
			y += PartitionValues.getWidth() / 2;
			drawSoupir(g, x, y);
			g.fillRect(x + PartitionValues.getWidth() + 2,
					y + PartitionValues.getWidth(),
					PartitionValues.getWidth() / 4,
					PartitionValues.getWidth() / 4);
		} else {
			g.setFont(new Font("Arial", Font.ITALIC, 16));
			g.drawChars(label.toCharArray(), 0, label.length(), x, y);
		}

		y -= 3 * PartitionValues.getWidth();

		circle(g);
		// paintLabel(g, 20);
	}

	private void drawDemiSoupir(Graphics g, int x, int y) {
		int width = PartitionValues.getWidth() - 2;
		g.fillOval(x, y + 1, width, width);
		int x1 = x + width / 2;
		int y1 = y + width;
		int x2 = x + PartitionValues.getWidth() + PartitionValues.getWidth()
				/ 2;
		int y2 = y;
		int x3 = x + PartitionValues.getWidth() / 2;
		int y3 = y + 2 * PartitionValues.getWidth();

		g.drawLine(x1, y1, x2, y2);
		g.drawLine(x2, y2, x3, y3);

	}

	private void drawSoupir(Graphics g, int x, int y) {
		int pad = PartitionValues.getWidth() / 2;
		int x1 = x;
		int x2 = x1 + pad;
		int x3 = x2 + pad;
		int x1bis = x + pad / 2;

		int y1 = y;
		int y2 = y1 + pad;
		int y3 = y2 + pad;
		int y4 = y3 + pad;
		int y5 = y4 + pad;
//		int y5bis = y5 + pad / 2;
		int y6 = y5 + pad;
		int y7 = y6 + pad;
//		int y8 = y7 + pad;

		int[] xPoints1 = { x1, x3, x2, x3, x1, x2 };

		int[] yPoints1 = { y1, y3, y5, y6, y4, y2 };

		g.fillPolygon(xPoints1, yPoints1, 6);
		g.drawPolygon(xPoints1, yPoints1, 6);
		// g.drawArc(x2, y6, PartitionValues.getWidth(),
		// PartitionValues.getWidth(), 90, 90);

		int[] xPoints2 = { x3, x1, x1, x1bis, x3 };

		int[] yPoints2 = { y6, y5, y7, y6, y6 };
		g.fillPolygon(xPoints2, yPoints2, 5);

	}
}
